home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-10-14 | 6.8 KB | 295 lines | [TEXT/CWIE] |
- // =================================================================================
- // Copyright © 1996 Michael Schürig
- //
- // You may copy this file, rip it apart or use it in derivative work as long
- // as you don't change the original file and this message remains intact.
- // =================================================================================
-
- #include <ansi_prefix.mac.h>
- #include <cctype>
- #include <iostream>
- #include <UException.h>
- #include "TMPLParser.h"
-
-
- // As of now I can't get the MSL omanip to work, so here's the workaround
-
- class indent {
-
- friend ostream &operator<< (ostream &, indent const &);
-
- private:
- UInt16 mIndent;
-
- public:
- indent(const UInt16 ind): mIndent(ind) {}
- };
-
-
- UInt8 HexToInt(const char inDigit);
-
- ostream &operator<< (ostream &, ConstStr255Param);
-
-
-
- TMPLParser::TMPLParser(
- const Handle inTMPLH)
- throw()
- : UResourceParser(inTMPLH)
- {
-
- }
-
- TMPLParser::~TMPLParser()
- throw()
- {
- }
-
- void
- TMPLParser::Parse()
- {
- }
-
- void
- TMPLParser::WriteParser(const string &inParserName, ostream & os)
- {
- UInt16 listCount = 0;
- UInt16 indLvl = 1;
-
- os << "#include \"UResourceParser.h\"" << endl << endl;
- os << "class " << inParserName << ": public UResourceParser {" << endl;
- os << "public:" << endl;
- os << "\t\t\t\t\t" << inParserName << "(const Handle inResH) throw();" << endl;
- os << "\tvirtual\t\t\t~" << inParserName << "() throw();" << endl << endl;
- os << "\tvirtual void\tParse();" << endl;
- os << "};" << endl << endl << endl << endl;
-
-
- os << "void" << endl << inParserName << "::Parse()" << endl << "{" << endl;
-
- UResourceParser::ListUntilEnd itemIter(this);
-
- while (itemIter.Advance())
- {
- StringPtr label;
- OSType fieldType;
-
- label = PSTR();
- fieldType = TNAM();
-
-
- switch (fieldType)
- {
- // Alignments
- case 'AWRD':
- os << indent(indLvl) << "AWRD();" << endl;
- break;
- case 'ALNG':
- os << indent(indLvl) << "ALNG();" << endl;
- break;
-
- // Fillers
- case 'FBYT':
- os << indent(indLvl) << "FBYT();" << endl;
- break;
- case 'FWRD':
- os << indent(indLvl) << "FWRD();" << endl;
- break;
- case 'FLNG':
- os << indent(indLvl) << "FLNG();" << endl;
- break;
-
- // Integers
- case 'DBYT':
- case 'HBYT':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "SBYT();" << endl;
- break;
- case 'DWRD':
- case 'HWRD':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "SWRD();" << endl;
- break;
- case 'DLNG':
- case 'HLNG':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "SLNG();" << endl;
- break;
-
- // Pascal strings
- case 'PSTR':
- case 'ESTR':
- case 'OSTR':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "PSTR();" << endl;
- break;
-
- // C strings
- case 'CSTR':
- case 'ECST':
- case 'OCST':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "CSTR();" << endl;
- break;
-
- // Hex dump
-
- case 'HEXD':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "HEXD();" << endl;
- break;
-
- // Misc
-
- case 'BOOL':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "BOOL();" << endl;
- break;
-
- case 'BBIT': {
- UInt8 i = 7;
- os << endl << indent(indLvl) << "// " << label;
- while(i--)
- {
- label = PSTR();
-
- OSType theBit = TNAM();
- if (theBit != 'BBIT')
- throw "Illegal number of BBITs";
-
- os << endl << indent(indLvl) << "// " << label;
- }
- os << endl << indent(indLvl) << "BBIT();" << endl;
-
- break;
- }
- case 'CHAR':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "CHAR();" << endl;
- break;
-
- case 'RECT':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "RECT();" << endl;
- break;
-
- case 'TNAM':
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "TNAM();" << endl;
- break;
-
-
- // Lists
-
- case 'LSTB':
- ++listCount;
- os << indent(indLvl) << "UResourceParser::ListUntilEnd\titer" << listCount << ";" << endl;
- os << indent(indLvl) << "while (iter" << listCount << ".Advance())" << endl;
- os << indent(indLvl) << '{';
- ++indLvl;
- break;
-
- case 'LSTZ':
- ++listCount;
- os << indent(indLvl) << "UResourceParser::ListUntilZero\titer" << listCount << ";" << endl;
- os << indent(indLvl) << "while (iter" << listCount << ".Advance())" << endl;
- os << indent(indLvl) << '{';
- ++indLvl;
- break;
-
- case 'OCNT':
- ++listCount;
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "UResourceParser::ListOneBased\titer" << listCount << ";" << endl;
- os << indent(indLvl) << "while (iter" << listCount << ".Advance())" << endl;
- os << indent(indLvl) << '{';
- ++indLvl;
- break;
-
- case 'ZCNT':
- ++listCount;
- os << endl << indent(indLvl) << "// " << label << endl;
- os << indent(indLvl) << "UResourceParser::ListZeroBased\titer" << listCount << ";" << endl;
- os << indent(indLvl) << "while (iter" << listCount << ".Advance())" << endl;
- os << indent(indLvl) << '{';
- ++indLvl;
- break;
-
- case 'LSTC':
- break;
-
- case 'LSTE':
- --indLvl;
- os << indent(indLvl) << '}' << endl;
- break;
-
-
- default: { // Fixed-length strings and hexdumps
- UInt8 flType[4];
- UInt16 sz;
-
- BlockMoveData(&fieldType, &flType, sizeof(fieldType));
-
- switch(flType[0])
- {
- case 'P':
- os << endl << indent(indLvl) << "// " << label << endl;
- sz = (HexToInt(flType[2]) * 16) + HexToInt(flType[3]);
- os << indent(indLvl) << "P0nn(" << sz << ");" << endl;
- break;
- case 'C':
- os << endl << indent(indLvl) << "// " << label << endl;
- sz = ((HexToInt(flType[1]) * 16) + (HexToInt(flType[2])) * 16)
- + HexToInt(flType[3]);
- os << indent(indLvl) << "Cnnn(" << sz << ");" << endl;
- break;
- case 'H':
- os << endl << indent(indLvl) << "// " << label << endl;
- sz = ((HexToInt(flType[1]) * 16) + (HexToInt(flType[2])) * 16)
- + HexToInt(flType[3]);
- os << indent(indLvl) << "Hnnn(" << sz << ");" << endl;
- break;
-
- default:
- throw "Unknown field type.";
- break;
- }
-
- break;
- }
- }
- }
- os << '}' << endl;
- }
-
-
- ostream &operator<< (ostream &os, indent const &ind)
- {
- for (UInt16 i = ind.mIndent; i > 0; --i)
- os.put('\t');
- return os;
- }
-
- ostream &operator<< (ostream &os, ConstStr255Param s)
- {
- os.write(static_cast<const char *>(s) + 1, StrLength(s));
- return os;
- }
-
- UInt8
- HexToInt(
- const char inDigit)
- {
- UInt8 theValue;
- char upDigit = toupper(inDigit);
-
- if ('0' <= inDigit && inDigit <= '9')
- theValue = inDigit - '0';
- else if ('A' <= upDigit && upDigit <= 'F')
- theValue = upDigit - 'A';
- else
- throw "Illegal hex digit.";
-
- return theValue;
- }
-